<template>
  <div class="app-container">
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="值班状态" prop="onDutyStatus">
        <el-select
          v-model="queryParams.onDutyStatus"
          placeholder="请选择值班状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in onDutyStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="上次登陆时间" prop="lastLogin">
        <el-date-picker
          v-model="queryParams.lastLogin"
          clearable
          size="small"
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择上次登陆时间"
        />
      </el-form-item> -->
      <!--      <el-form-item label="登陆ip" prop="loginIp">
        <el-input
          v-model="queryParams.loginIp"
          placeholder="请输入登陆ip"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!--      <el-form-item label="医院标识" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入医院标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!--      <el-form-item label="账户标识" prop="cid">
        <el-input
          v-model="queryParams.cid"
          placeholder="请输入账户标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['manager:dispatcher:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['manager:dispatcher:edit']"
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['manager:dispatcher:remove']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['manager:dispatcher:export']"-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table
      v-loading="loading"
      :data="dispatcherList"
      @selection-change="handleSelectionChange"
      height="67vh"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="登陆密码" align="center" prop="password" />-->
      <el-table-column label="登录账号" align="center" prop="accountName" />
      <!--<el-table-column label="调度员标识" align="center" prop="dispatcherID" />-->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="所属医院" align="center" prop="hospital" />
      <el-table-column
        label="值班状态"
        align="center"
        prop="onDutyStatus"
        :formatter="onDutyStatusFormat"
      />
      <el-table-column
        label="上次登陆时间"
        align="center"
        prop="lastLogin"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastLogin, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="登陆ip" align="center" prop="loginIp" /> -->
      <!-- <el-table-column label="医院标识" align="center" prop="uid" /> -->
      <!-- <el-table-column label="账户标识" align="center" prop="cid" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['manager:dispatcher:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-hasPermi="['manager:dispatcher:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改调度员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="登录账号" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入账户" />
        </el-form-item>
        <el-form-item label="登陆密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入登陆密码" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <!--        <el-form-item label="所属医院" prop="hospital">
          <el-input v-model="form.hospital" placeholder="请输入所属医院" />
        </el-form-item> -->
        <!--        <el-form-item label="上次登陆时间" prop="lastLogin">
          <el-date-picker
            v-model="form.lastLogin"
            clearable
            size="small"
            style="width: 200px"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择上次登陆时间"
          />
        </el-form-item> -->
        <!--        <el-form-item label="登陆ip" prop="loginIp">
          <el-input v-model="form.loginIp" placeholder="请输入登陆ip" />
        </el-form-item> -->
        <!--        <el-form-item label="医院标识" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入医院标识" />
        </el-form-item> -->
        <!--        <el-form-item label="账户标识" prop="cid">
          <el-input v-model="form.cid" placeholder="请输入账户标识" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDispatcher,
  getDispatcher,
  delDispatcher,
  addDispatcher,
  updateDispatcher,
  exportDispatcher
} from "@/api/biz/dispatcher";

export default {
  name: "Dispatcher",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 调度员管理表格数据
      dispatcherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 值班状态字典
      onDutyStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        password: undefined,
        accountName: undefined,
        name: undefined,
        phone: undefined,
        address: undefined,
        onDutyStatus: undefined,
        lastLogin: undefined,
        loginIp: undefined,
        uid: undefined,
        cid: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
        address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
        onDutyStatus: [
          { required: true, message: "值班状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("doctor_duty_status").then(response => {
      this.onDutyStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询调度员管理列表 */
    getList() {
      this.loading = true;
      listDispatcher(this.queryParams).then(response => {
        this.dispatcherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 值班状态字典翻译
    onDutyStatusFormat(row, column) {
      return this.selectDictLabel(this.onDutyStatusOptions, row.onDutyStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        password: undefined,
        accountName: undefined,
        dispatcherID: undefined,
        name: undefined,
        phone: undefined,
        address: undefined,
        remark: undefined,
        onDutyStatus: undefined,
        hospital: undefined,
        lastLogin: undefined,
        loginIp: undefined,
        uid: undefined,
        cid: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dispatcherID);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加调度员管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dispatcherID = row.dispatcherID || this.ids;
      getDispatcher(dispatcherID).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改调度员管理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dispatcherID !== undefined) {
            updateDispatcher(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addDispatcher(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dispatcherIDs = row.dispatcherID || this.ids;
      this.$confirm(
        '是否确认删除调度员管理编号为"' + dispatcherIDs + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delDispatcher(dispatcherIDs);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有调度员管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportDispatcher(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
<style scoped="scoped">
/* 分页 */
.pagination-container {
  height: 80px;
  margin-top: 0px;
  padding: 23px 20px !important;
}
/* 分页  end */
</style>
